package co.com.smart.eagle.bean.user;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.PersistenceException;

import co.com.smart.eagle.entities.EagleSecurityRoles;
import co.com.smart.eagle.entities.EagleSecurityUsers;
import co.com.smart.eagle.infrastructure.dto.RolesDTO;
import co.com.smart.eagle.infrastructure.dto.SmartExcepcionSerializada;
import co.com.smart.eagle.infrastructure.dto.UsuariosDTO;
import co.com.smart.eagle.mapper.Mapper;
import co.com.smart.eagle.persistence.entity.UserFacadeLocal;
import co.com.smart.eagle.session.DelegateContextEJB;

/**
 * Session Bean implementation class UserBean
 */
@Stateless(mappedName = "ejb/UserBean")
public class UserBean implements UserBeanLocal {

	private UserFacadeLocal facade;

	/**
	 * Default constructor.
	 */
	public UserBean() {
		facade = DelegateContextEJB.getUserFacadeLocal();
	}

	@Override
	public List<UsuariosDTO> getUsers() {
		List<UsuariosDTO> user = new ArrayList<UsuariosDTO>();

		List<EagleSecurityUsers> usersEntity = facade.findAll();

		for (EagleSecurityUsers eagleSecurityUsers : usersEntity) {
			UsuariosDTO usuario = new UsuariosDTO();
			List<RolesDTO> roles = new ArrayList<RolesDTO>();
			usuario.setDsUser(eagleSecurityUsers.getUsername());
			usuario.setDsClave(eagleSecurityUsers.getUsername());

			List<EagleSecurityRoles> rolesEntity = eagleSecurityUsers.getEagleSecurityRolesList();
			if (rolesEntity != null) {
				for (EagleSecurityRoles eagleSecurityRoles : rolesEntity) {
					roles.add(Mapper.mapperRoles(eagleSecurityRoles));
				}
			}
			usuario.setRoles(roles);
			user.add(usuario);
		}

		return user;
	}

	@Override
	public void setUser(UsuariosDTO user) throws Exception{
		if (user.isEdited()) {
			EagleSecurityUsers userEntity = new EagleSecurityUsers(user.getDsUser(),user.getDsClave());
			List<EagleSecurityRoles> eagleSecurityRolesList = new ArrayList<EagleSecurityRoles>();

			List<RolesDTO> roles = user.getRoles();
			for (RolesDTO rolesDTO : roles) {
				eagleSecurityRolesList.add(Mapper.mapperEntityRol(rolesDTO));
			}
			userEntity.setEagleSecurityRolesList(eagleSecurityRolesList);
			facade.edit(userEntity);
		} else {
			EagleSecurityUsers userEntity = facade.find(user.getDsUser());
			if (userEntity != null) {
				throw new SmartExcepcionSerializada("El usuario ya existe, por favor usar otro");
			}
			userEntity = Mapper.mapperEntityUser(user);
			List<EagleSecurityRoles> rolesEntity = new ArrayList<EagleSecurityRoles>();
			List<RolesDTO> roles = user.getRoles();
			if (roles != null && !roles.isEmpty() && roles.size() > 0) {
				for (RolesDTO rolesDTO : roles) {
					rolesEntity.add(Mapper.mapperEntityRol(rolesDTO));
				}
				userEntity.setEagleSecurityRolesList(rolesEntity);
			}
			facade.create(userEntity);
		}
	}
	
	@Override
	public void deleteUser(UsuariosDTO user) throws Exception {
		try{
			EagleSecurityUsers userEntity = new EagleSecurityUsers(user.getDsUser());
			facade.remove(userEntity);
		} catch (PersistenceException ex) {
			throw new SmartExcepcionSerializada("No es posible eliminar el registro, Dependencia de datos");
		}
	}

}
